(function(){
    _$(document).ready(function(){
        function App(){};
        var page=0;
        var type="updateTime";
        App.prototype.ajaxFn=function(){
            //初始化获取书籍
            initBook();
        };
        function initBook(){
            _$.ajax({
                type: 'post',
                url: '/getCateBook',
                data: {type:type,catid:catid, page: 0},
                async: false,
                success: function (json) {
                    var dropload = $('#list').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已无数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me) {
                            var pageLength = json['count'];
                            if (json['count'] % 10 > 0) {
                                pageLength = (json['count'] / 10) + 1;
                            }
                            if (page > pageLength) {
                                me.lock();
                                me.noData();
                                me.resetload();
                            } else {
                                console.log(page);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getCateBook',
                                    data: {type:type,catid:catid, page: page},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            var info = data['info'];
                                            if (info == "") {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            } else {
                                                if (info.length > 0) {
                                                    var html = "";
                                                    for (var val in info) {
                                                        info[val]['index'] = parseInt(val) + 1;
                                                        if (info[val]['stateType'] == "T") {
                                                            info[val]['stateType'] = "连载中";
                                                        } else if (info[val]['stateType'] == "F") {
                                                            info[val]['stateType'] = "完结";
                                                        }
                                                        if (info[val]['cnt'] > 10000) {
                                                            info[val]['cnt'] = (info[val]['cnt'] / 10000).toFixed(2) + "万字";
                                                        } else {
                                                            info[val]['cnt'] = info[val]['cnt'] + "字";
                                                        }
                                                        html += template('tpl', info[val]);
                                                    }
                                                    // 为了测试，延迟1秒加载
                                                    setTimeout(function () {
                                                        // 插入加载使用 html() 重置 DOM
                                                        _$("#list .dropload-down").before(html);
                                                        page++;
                                                        // 每次数据加载完，必须重置
                                                        me.resetload();
                                                    }, 1000);
                                                }
                                            }
                                        }else {
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    })
                }
            });
        }
        App.prototype.eventFn=function(){
            _$(".toplist-tag a").click(function(){
                _$(this).addClass('active').siblings('a').removeClass('active');
                var value=_$(this).attr('data-value');
                $('#list').html("");
                $(window).scrollTop(0);
                type=value;
                page=0;
                initBook();
            });
        };
        App.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();
        };
        //程序初始化
        new App().init();
    })
})();